﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace BilingualSentSearchEngine
{
    class ConciseDict
    {
        Dictionary<string, int> chinese2Id = new Dictionary<string, int>();
        Dictionary<string, int> english2Id = new Dictionary<string, int>();

        Dictionary<int, List<int>> englishId2ChineseIdList = new Dictionary<int, List<int>>();
        Dictionary<int, List<int>> chineseId2EnglishIdList = new Dictionary<int, List<int>>();

        List<string> chineseDictionary = new List<string>();
        List<string> englishDictionary = new List<string>();


        public ConciseDict(string englishDictionaryFileName, string chineseDictionaryFileName, string DictionaryIndexFileName)
        {
            int cnt = 0;
            string line;
            StreamReader sr = new StreamReader(chineseDictionaryFileName);
            while (sr.EndOfStream == false)
            {
                line = sr.ReadLine();
                chineseDictionary.Add(line);
                chinese2Id.Add(line, cnt);
                cnt++;
            }
            sr.Close();

            cnt = 0;
            sr = new StreamReader(englishDictionaryFileName);
            while (sr.EndOfStream == false)
            {
                line = sr.ReadLine();
                englishDictionary.Add(line);
                english2Id.Add(line, cnt);
                cnt++;
            }
            sr.Close();

            sr = new StreamReader(DictionaryIndexFileName);
            BinaryReader br = new BinaryReader(sr.BaseStream);
            while (br.BaseStream.Position < br.BaseStream.Length)
            {
                int chineseId, englishId;
                chineseId = br.ReadInt32();
                englishId = br.ReadInt32();

                if (englishId2ChineseIdList.ContainsKey(englishId) == false)
                {
                    englishId2ChineseIdList.Add(englishId, new List<int>());
                }
                englishId2ChineseIdList[englishId].Add(chineseId);

                if (chineseId2EnglishIdList.ContainsKey(chineseId) == false)
                {
                    chineseId2EnglishIdList.Add(chineseId, new List<int>());
                }
                chineseId2EnglishIdList[chineseId].Add(englishId);
            }
            br.Close();

        }

        public List<string> GetEnglishByChinese(string strChinese)
        {
            int chineseId;

            if (chinese2Id.ContainsKey(strChinese) == true)
            {
                chineseId = chinese2Id[strChinese];

                List<string> resultList = new List<string>();
                foreach (int id in chineseId2EnglishIdList[chineseId])
                {
                    resultList.Add(englishDictionary[id]);
                }

                return resultList;
            }

            return new List<string>();
        }

        public List<string> GetChineseByEnglish(string strEnglish)
        {
            int englishId;

            if (english2Id.ContainsKey(strEnglish) == true)
            {
                englishId = english2Id[strEnglish];

                List<string> resultList = new List<string>();
                foreach (int id in englishId2ChineseIdList[englishId])
                {
                    resultList.Add(chineseDictionary[id]);
                }

                return resultList;
            }

            return new List<string>();

        }
    }
}
